package com.th.therp.controller;

import com.th.therp.exception.BaseException;
import com.th.therp.pojo.dto.UpdateUserDto;
import com.th.therp.pojo.result.Result;
import com.th.therp.pojo.vo.UserVo;
import com.th.therp.service.UserService;
import jakarta.validation.Valid;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员请求控制器
 */
@Log4j2
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @GetMapping("/page")
    @ResponseBody
    public Result<List<UserVo>> list(){
        log.info("查询全部用户");
        List<UserVo> userVoList = userService.list();
        return Result.success(userVoList);
    }

    @PutMapping
    @ResponseBody
    public Result update(@Valid @RequestBody UpdateUserDto updateUserDto, BindingResult bindingResult){
        log.info("修改用户信息");
        if (bindingResult.hasErrors()){
            throw new BaseException("格式不正确");
        }
        userService.update(updateUserDto);
        return Result.success();
    }

    @DeleteMapping
    @ResponseBody
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除用户:{}",ids);
        userService.delete(ids);
        return Result.success();
    }
}
